/*
	DSA Project on use of arrays and linked lists to obtain the same functionality as the functions of list data structure in Python
*/


#include <stdio.h>
#include<malloc.h>
#include<math.h>

/*
	list.append(x)
	Add an item to the end of the list; equivalent to a[len(a):] = [x].
*/

int append(int *list,int var,int buff)
{ 
	int i;
	list=(int*)realloc(list,(buff+1)*sizeof(int));//The realloc() function changes the size of a block of memory that we earlier allocated with 		malloc()
	list[buff]=var;//Consider size of the list(array is what we are talking about)
	for (i=0;i < buff+1;i++)
	{
		printf("%d ", list[i]);
	}
}


/*
	list.sort()
    	Sort the items of the list, in place.
*/
int sortinc(int *list1, int buff)
{
	/*
		Here we are taking an element and then comparing it with all the other elements to check if it is bigger or smaller than them and that 			is how we do sorting. We then decide if we want to sort the array in an increasing or decreasing order based on user choice. This 			algorithm is doing the sorting in place.
	*/ 	
	
	int i,j;
	int sort=0;
	int option;
	
	for(i=0;i<buff;i++)
	{
		for(j=i+1;j<buff;j++)
		{
			if(list1[j]<list1[i])//in case that is the case then we swap the values
			{
				sort = list1[i];
				list1[i] = list1[j];
			 	list1[j] = sort;
			}
		}	
	}
	for (i=0;i<buff;i++)
	{
		printf("%d  ", list1[i]);
	}
	
	
}

int sortdec(int *list2,int buff)
{
	/*
		Here we are taking an element and then comparing it with all the other elements to check if it is bigger or smaller than them and that 			is how we do sorting. We then decide if we want to sort the array in an increasing or decreasing order based on user choice. This 			algorithm is doing the sorting in place.
	*/ 	
	
	int i,j;
	int sort=0;
	int option;
	
	for(i=0;i<buff;i++)
	{
		for(j=i+1;j<buff;j++)
		{
			if(list2[j]>list2[i])//in case that is the case then we swap the values
			{
				sort = list2[i];
				list2[i] = list2[j];
			 	list2[j] = sort;
			}
		}	
	}
	for (i=0;i<buff;i++)
	{
		printf("%d  ", list2[i]);
	}
}


int main ()
{
	int *list,buff,var,choice;
	int i;
	int j=0;//this declaration is done only so that it can be used by us in ensuring that the user gets options to do what he wants any number of 		times
	printf("enter the size of the array");
	scanf("%d",&buff);
	list=(int*)(malloc(buff*sizeof(int)));
	for (i=0; i<buff;i++)
	{printf("enter the %d element ", i+1);
	scanf("%d", &list[i]); }
	while(j==0)
	{
	printf("enter your option\n0.exit\n1.append\n2.sort in increasing order\n3.sort in decreasing order\n");
	scanf("%d",&choice);
	switch(choice)
	{ 
		case 0: exit(0);
			break;
		case 1: printf("enter the no you want to append at the end of the list");
			scanf("%d",&var);
		   	append(list,var,buff);
		   	buff =buff+1;
		   	break;
		case 2: sortinc(list,buff);
			break;
		case 3: sortdec(list,buff);
			break;
	
	}
	}
}

//References: http://www.java-samples.com/showtutorial.php?tutorialid=589 	




